
********************************************************************************
*
* GRIST TO THE MILL OF SUBVERSION:
* STRIKES AND COUPS IN COUNTERINSURGENCIES
*
* By Christian Glaessel, Belén González, Adam Scharpf
*
* European Journal of International Relations
*
* April 2020
*
********************************************************************************
*
* Content:	Replication of figures in manuscript and supporting information	
*
*			Steps:
*			1) Set working directory
*			2) Run do-file to replicate figures in manuscript and supporting information
*
********************************************************************************
*
* Packages: Before running this code, please make sure you have the following
*			.ado-files installed
*
*			- Clarify Package, Version 2.1:
*					net from https://gking.harvard.edu/clarify 
*					net install clarify 
*			- CEM Package, Version 10.1: 
*					ssc install cem
*			- Parmest Package, Version 2
*					net install st0043_2.pkg
*
********************************************************************************

	version 15.1
	set more off

	clear all
  
 
 
********************************************************************************
*
* Setting working directory in macro
*
********************************************************************************

	*Insert path to folder containing dataset "StrikesCoups_analysis.dta" before running
	global path="..." //Example: "C:\Users\me\Desktop\Glaesseletal_StrikesCoups"
	
 
 
********************************************************************************
*
* Producing log-file of results
*
********************************************************************************

	*Starting log file
	log using "$path\Glaesseletal_StrikesCoups_EJIR_figures", replace 

	
	
********************************************************************************
*
* Loading replication dataset
*
********************************************************************************

	*Loading replication dataset
	use "$path\StrikesCoups_analysis.dta"


	
********************************************************************************
*
* Descriptive statistics
*
********************************************************************************

	****************************************************************************
	*Figure 2. Temporal order of resistance events and coup attempts
	****************************************************************************

	*see readme.txt and Replication_figure2.do
	
	

********************************************************************************
*
* Empirical results
*
********************************************************************************
	
	****************************************************************************
	*Figure 3. Substantive effects of different resistance types on the risk of 
	*coup attempts in counterinsurgencies (based on Model 3)
	****************************************************************************	
	
	*Preserving dataset
	preserve
	
	*Generating x-axis (0 <-> 5)
	gen float xaxis=_n-1 in 1/6

	*Generating variable containers for quantities of interest
	foreach var of varlist banks_guerilla banks_riots banks_strikes banks_demos {
	gen prlo1_`var'=.
	gen prhi1_`var'=.
	gen prpo1_`var'=.
	}

	*Estimating Model 3
	set seed 8413275
	#delimit ;
	estsimp 
	logit 	coup
			banks_guerilla banks_riots
			banks_strikes banks_demos
			time time2 time3
			changergdp
			logtroopqual
			cheibub_democracy
			ldis
			multcoins
			occup
			if coin == 1, 
			cluster(gwno)
	;
	# delimit cr

	*Predicted probabilities for guerrilla attacks
	setx banks_riots 0 banks_strikes 0 banks_demos 0 cheibub_democracy 0 occup 0 logtroopqual p25 (time time2 time3 changergdp ldis multcoins) median
	local a=0 
	while `a'<=5 {
		setx banks_guerilla `a'
		simqi, prval(1) genpr(pr1) listx
		_pctile pr1, p(2.5,97.5)	
		replace prlo1_banks_guerilla=r(r1) if xaxis==float(`a')
		replace prhi1_banks_guerilla=r(r2) if xaxis==float(`a')
		egen p = mean(pr1)
		replace prpo1_banks_guerilla = p if xaxis==float(`a')
		drop pr1 p
		local a=`a'+1
	}

	*Predicted probabilities for riots
	setx banks_guerilla 0 banks_strikes 0 banks_demos 0 cheibub_democracy 0 occup 0 logtroopqual p25 (time time2 time3 changergdp ldis multcoins) median
	local a=0 
	while `a'<=5 {
		setx banks_riots `a'
		simqi, prval(1) genpr(pr1) listx
		_pctile pr1, p(2.5,97.5)	
		replace prlo1_banks_riots=r(r1) if xaxis==float(`a')
		replace prhi1_banks_riots=r(r2) if xaxis==float(`a')
		egen p = mean(pr1)
		replace prpo1_banks_riots = p if xaxis==float(`a')
		drop pr1 p
		local a=`a'+1
	}

	*Predicted probabilities for strikes
	setx banks_guerilla 0 banks_riots 0 banks_demos 0 cheibub_democracy 0 occup 0 logtroopqual p25 (time time2 time3 changergdp ldis multcoins) median
	local a=0 
	while `a'<=5 {
		setx banks_strikes `a'
		simqi, prval(1) genpr(pr1) listx
		_pctile pr1, p(2.5,97.5)	
		replace prlo1_banks_strikes=r(r1) if xaxis==float(`a')
		replace prhi1_banks_strikes=r(r2) if xaxis==float(`a')
		egen p = mean(pr1)
		replace prpo1_banks_strikes = p if xaxis==float(`a')
		drop pr1 p
		local a=`a'+1
	}

	*Predicted probabilities for demonstrations
	setx banks_guerilla 0 banks_strikes 0 banks_riots 0 cheibub_democracy 0 occup 0 logtroopqual p25 (time time2 time3 changergdp ldis multcoins) median
	local a=0 
	while `a'<=5 {
		setx banks_demos `a'
		simqi, prval(1) genpr(pr1) listx
		_pctile pr1, p(2.5,97.5)	
		replace prlo1_banks_demos=r(r1) if xaxis==float(`a')
		replace prhi1_banks_demos=r(r2) if xaxis==float(`a')
		egen p = mean(pr1)
		replace prpo1_banks_demos = p if xaxis==float(`a')
		drop pr1 p
		local a=`a'+1
	}

	*Keeping relevant variables
	keep xaxis pr*
	keep in 1/6

	*Generating "shifted" x-axes for plotting
	gen xguerrs=xaxis+0.07
	gen xriots=xaxis+0.19
	gen xdemos=xaxis-0.07
	gen xstrikes=xaxis-0.19

	*Graph
	#delimit ;		
	  twoway rspike prlo1_banks_strikes prhi1_banks_strikes xstrikes, 
					 fcolor(black) lcolor(black) fintensity(100)
		  || scatter prpo1_banks_strikes xstrikes,
					 msymbol(o) msize(*1) mfcolor(white) mlcolor(black) mlwidth(.3) mlalign(outside)
		  || rspike prlo1_banks_demos prhi1_banks_demos xdemos, 
					 fcolor(black) lcolor(black) fintensity(100)			 
		  || scatter prpo1_banks_demos xdemos,
					 msymbol(s) msize(*1.5) mfcolor(white) mlcolor(black)				 
		  || rspike prlo1_banks_guerilla prhi1_banks_guerilla xguerrs, 
					 fcolor(gray) lcolor(black) fintensity(100)
		  || scatter prpo1_banks_guerilla xguerrs,
					 msymbol(o) msize(*1.5) mfcolor(black) mlcolor(black)
		  || rspike prlo1_banks_riots prhi1_banks_riots xriots, 
					 fcolor(gray) lcolor(black) fintensity(100)
		  || scatter prpo1_banks_riots xriots,
					 msymbol(s) msize(*1.5) mfcolor(black) mlcolor(black)
					 yline(0, lpattern(dot) lcolor(gs8))
					 xlabel(none) 
					 xscale(range(-0.5 5.5) noline titlegap(*+5))
					 yscale(range(0 1) noline)				 
					 yticks(0(.1).8, tlength(*.5))
					 ylabel(0(.2).8, angle(horizontal) nogrid labsize(medium))
					 xlabel(0(1)5, angle(horizontal) nogrid labsize(medium))
					 xtitle("Number of resistance events", size(medium)) 
					 ytitle("Pr(Coup)", size(medium))
					 ysize(1.5) xsize(2)
					 subtitle(,size(2.5) lcolor(black))
					 legend(order(2 "Strikes" 6 "Guerrilla attacks" 1 "95% CI" 4 "Demonstrations" 8 "Riots") position(2) ring(0) bmargin(0) col(3) row(2) 
					 size(small) keygap(*0.5) symxsize(*0.25) symysize(*0.25) region(lcolor(black)))
					 scheme(s2mono) plotregion(lcolor(black)) graphregion(color(white) lcolor(white) lalign(outside)); 
	#delimit cr
	graph export "$path\figure3.pdf", replace as(pdf)

	*Restoring data
	restore
		
	
	
	****************************************************************************
	*Figure 4. Conditional effects of different resistance types on the risk of 
	*coup attempts in insurgencies and peace times (based on Model 3, Table SI.9)
	****************************************************************************		
	
	*Preserving dataset
	preserve
	
	*Model 3
	set seed 8413275
	#delimit ;
	estsimp 
	logit 	coup
			c_coin_strike
			c_coin_demons
			c_coin_guerr
			c_coin_riot
			banks_strikes 
			banks_demos
			banks_guerilla 
			banks_riots
			coin
			time time2 time3
			changergdp
			logtroopqual
			cheibub_democracy		
			, 
			cluster(gwno)
	;
	# delimit cr


	* Effect of strikes 

	*Pr(Y) for strikes == 5 and COIN == 0
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	0
			banks_riots		0
			coin			0
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0		
			;
	setx	;
	simqi, prval(1) genpr(coin_strike_00) ;
	#delimit cr

	*Pr(Y) for strikes == 5 and COIN == 1
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	0
			banks_riots		0
			coin			1
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0			
			;
	setx	;
	simqi, prval(1) genpr(coin_strike_10) ;
	#delimit cr

	*Pr(Y) for strikes == 5 and COIN == 0
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	5
			banks_demos		0
			banks_guerilla 	0
			banks_riots		0
			coin			0
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0				
			;
	setx	;
	simqi, prval(1) genpr(coin_strike_01) ;
	#delimit cr

	*Pr(Y) for strikes == 5 and COIN == 1
	#delimit ;
	setx 
			c_coin_strike	5
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	5
			banks_demos		0
			banks_guerilla 	0
			banks_riots		0
			coin			1
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0				
			;
	setx	;
	simqi, prval(1) genpr(coin_strike_11);
	#delimit cr


	* Effect of demonstrations 

	*Pr(Y) for demonstrations == 5 and COIN == 0
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	0
			banks_riots		0
			coin			0
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0			
			;
	setx	;
	simqi, prval(1) genpr(coin_demons_00) ;
	#delimit cr

	*Pr(Y) for demonstrations == 5 and COIN == 1
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	0
			banks_riots		0
			coin			1
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0			
			;
	setx	;
	simqi, prval(1) genpr(coin_demons_10) ;
	#delimit cr

	*Pr(Y) for demonstrations == 5 and COIN == 0
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		5
			banks_guerilla 	0
			banks_riots		0
			coin			0
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0				
			;
	setx	;
	simqi, prval(1) genpr(coin_demons_01) ;
	#delimit cr

	*Pr(Y) for demonstrations == 5 and COIN == 1
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	5
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		5
			banks_guerilla 	0
			banks_riots		0
			coin			1
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0				
			;
	setx	;
	simqi, prval(1) genpr(coin_demons_11);
	#delimit cr


	* Effect of guerrilla attacks 

	*Pr(Y) for guerrilla attacks == 5 and COIN == 0
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	0
			banks_riots		0
			coin			0
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0			
			;
	setx	;
	simqi, prval(1) genpr(coin_guerr_00) ;
	#delimit cr

	*Pr(Y) for guerrilla attacks == 5 and COIN == 1
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	0
			banks_riots		0
			coin			1
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0				
			;
	setx	;
	simqi, prval(1) genpr(coin_guerr_10) ;
	#delimit cr

	*Pr(Y) for guerrilla attacks == 1 and COIN == 0
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	5
			banks_riots		0
			coin			0
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0			
			;
	setx	;
	simqi, prval(1) genpr(coin_guerr_01) ;
	#delimit cr

	*Pr(Y) for guerrilla attacks == 5 and COIN == 1
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	5
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	5
			banks_riots		0
			coin			1
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0				
			;
	setx	;
	simqi, prval(1) genpr(coin_guerr_11);
	#delimit cr

	
	* Effect of riots 

	*Pr(Y) for riots == 5 and COIN == 0
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	0
			banks_riots		0
			coin			0
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0			
			;
	setx	;
	simqi, prval(1) genpr(coin_riot_00) ;
	#delimit cr

	*Pr(Y) for riots == 5 and COIN == 1
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	0
			banks_riots		0
			coin			1
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0			
			;
	setx	;
	simqi, prval(1) genpr(coin_riot_10) ;
	#delimit cr

	*Pr(Y) for riots == 5 and COIN == 0
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		0
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	0
			banks_riots		5
			coin			0
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0				
			;
	setx	;
	simqi, prval(1) genpr(coin_riot_01) ;
	#delimit cr

	*Pr(Y) for riots == 5 and COIN == 1
	#delimit ;
	setx 
			c_coin_strike	0
			c_coin_demons	0
			c_coin_guerr	0
			c_coin_riot		5
			banks_strikes 	0
			banks_demos		0
			banks_guerilla 	0
			banks_riots		5
			coin			1
			(time time2 time3) median
			changergdp		median
			logtroopqual 	p25
			cheibub_democracy 0				
			;
	setx	;
	simqi, prval(1) genpr(coin_riot_11);
	#delimit cr


	*Calculating effect sizes
	local var "strike demons guerr riot"
	foreach v of local var {
	gen eff_`v'_coin0=coin_`v'_01-coin_`v'_00
	gen eff_`v'_coin1=coin_`v'_11-coin_`v'_10
	}


	*Calculating quantities of interest for plotting
	foreach var of varlist eff_* {
		
		//Container variables
		gen mean_`var'=.
		gen hi_`var'=.
		gen lo_`var'=.

		//Calculating means
		sum `var'
		replace mean_`var' = r(mean) in 1
		
		//Calculating 95%CIs
		_pctile `var', p(2.5,97.5)	
		replace hi_`var'=r(r1) in 1
		replace lo_`var'=r(r2) in 1
	}

	*Keeping relevant variables
	keep mean_* hi_* lo_*
	keep in 1

	*Reshaping data for plotting
	gen id=1 in 1
	reshape long mean_@ hi_@ lo_@, i(id) j() string

		rename mean_ mean
		rename hi_ hi
		rename lo_ lo

		gen coin=substr(_j,-1,.)
		destring coin, replace
		
		gen resist=1 if regexm(_j, "strike")
		replace resist=2 if regexm(_j, "demons")
		replace resist=3 if regexm(_j, "guerr")
		replace resist=4 if regexm(_j, "riot")
		
	
		*Context: Peace
		
		*Generating x-axis
		gen xaxis_peace=1 if resist==1 & coin==0
		replace xaxis_peace=2 if resist==2 & coin==0
		replace xaxis_peace=3 if resist==3 & coin==0
		replace xaxis_peace=4 if resist==4 & coin==0	
		
		
		*Graph
		#delimit ;		
		twoway rspike hi lo xaxis_peace if resist==1, 
					 fcolor(black) lcolor(black) fintensity(100)
		  || scatter mean xaxis_peace if resist==1,
					 msymbol(o) msize(*1) mfcolor(white) mlcolor(black) mlwidth(.3) mlalign(outside)
		  || rspike hi lo xaxis_peace if resist==2, 
					 fcolor(black) lcolor(black) fintensity(100)
		  || scatter mean xaxis_peace if resist==2,
					 msymbol(s) msize(*1.5) mfcolor(white) mlcolor(black)				 
		  || rspike hi lo xaxis_peace if resist==3, 
					 fcolor(gray) lcolor(black) fintensity(100)
		  || scatter mean xaxis_peace if resist==3,
					 msymbol(o) msize(*1.5) mfcolor(black) mlcolor(black)
		  || rspike hi lo xaxis_peace if resist==4, 
					 fcolor(gray) lcolor(black) fintensity(100)
		  || scatter mean xaxis_peace if resist==4,
					 yline(0, lpattern(dot) lcolor(gs8))
					 msymbol(s) msize(*1.5) mfcolor(black) mlcolor(black)
					 xlabel(none) 
					 xscale(range(0.5 4.5) noline titlegap(*+10))
					 yscale(range( ) noline)				 
					 yticks( , tlength(*.5))
					 ylabel(, angle(horizontal) nogrid labsize(medium))
					 xlabel(1 "Strikes" 2 "Demonstrations" 3 `" "Guerrilla" "attacks" "' 4 "Riots", angle(horizontal) nogrid labsize(medium))
					 xtitle("Resistance type", size(medium) color(black)) 
					 ytitle("Pr(Coup)", size(medium))
					 ysize(1.5) xsize(2)
					 title("During peace: Coup risk", size(*0.85) box bexpand margin(t=1.25))
					 legend(off)
					 scheme(s2mono) plotregion(lcolor(black)) graphregion(color(white) lcolor(white) lalign(outside))
					 saving("$path\interact_peace", replace)				 
			; 
		#delimit cr		
	
	
		*Context: Counterinsurgency

		*Generating x-axis
		gen xaxis_insur=1 if resist==1 & coin==1
		replace xaxis_insur=2 if resist==2 & coin==1
		replace xaxis_insur=3 if resist==3 & coin==1
		replace xaxis_insur=4 if resist==4 & coin==1	
		
		
		*Graph
		#delimit ;		
		twoway rspike hi lo xaxis_insur if resist==1, 
					 fcolor(black) lcolor(black) fintensity(100)
		  || scatter mean xaxis_insur if resist==1,
					 msymbol(o) msize(*1) mfcolor(white) mlcolor(black) mlwidth(.3) mlalign(outside)
		  || rspike hi lo xaxis_insur if resist==2, 
					 fcolor(black) lcolor(black) fintensity(100)
		  || scatter mean xaxis_insur if resist==2,
					 msymbol(s) msize(*1.5) mfcolor(white) mlcolor(black)				 
		  || rspike hi lo xaxis_insur if resist==3, 
					 fcolor(gray) lcolor(black) fintensity(100)
		  || scatter mean xaxis_insur if resist==3,
					 msymbol(o) msize(*1.5) mfcolor(black) mlcolor(black)
		  || rspike hi lo xaxis_insur if resist==4, 
					 fcolor(gray) lcolor(black) fintensity(100)
		  || scatter mean xaxis_insur if resist==4,
					 yline(0, lpattern(dot) lcolor(gs8))
					 msymbol(s) msize(*1.5) mfcolor(black) mlcolor(black)
					 xlabel(none) 
					 xscale(range(0.5 4.5) noline titlegap(*+10))
					 yscale(range( ) noline)				 
					 yticks( , tlength(*.5))
					 ylabel(, angle(horizontal) nogrid labsize(medium))
					 xlabel(1 "Strikes" 2 "Demonstrations" 3 `" "Guerrilla" "attacks" "' 4 "Riots", angle(horizontal) nogrid labsize(medium))
					 xtitle("Resistance type", size(medium) color(white)) 
					 ytitle("Pr(Coup)", size(medium))
					 ysize(1.5) xsize(2)
					 title("During insurgency: Coup risk", size(*0.85) box bexpand margin(t=1.25))
					 legend(off)
					 scheme(s2mono) plotregion(lcolor(black)) graphregion(color(white) lcolor(white) lalign(outside))
					 saving("$path\interact_insurgency", replace)
			; 
	#delimit cr	
		
	
	*Combining graphs
	#delimit ;
	graph combine "$path\interact_insurgency.gph" 
				  "$path\interact_peace.gph" 
					,
				  cols(1)
				  ysize(1.5) xsize(1)
				  graphregion(color(white) lcolor(white) lalign(outside))
				  ;
	#delimit cr
	graph export "$path\figure4.pdf", replace as(pdf)	
	
	*Restoring dataset
	restore
	
	
	
	****************************************************************************
	*Figure 5. Effects of resistance types on insurgency risk during peace times 
	*and on coup risk during insurgencies using probit models with Heckman sample 
	*selection (based on Model 3, Table SI.11)
	****************************************************************************		
	
	*Preserving dataset
	preserve
	
	*Saving model to macro: Model 3
	#delimit ;
	local model="heckprobit coup
				banks_strikes 
				banks_demos
				banks_guerilla 
				banks_riots			
				time time2 time3
				changergdp
				logtroopqual
				i.cheibub_democracy
				ldis
				i.multcoins
				i.occup			
				, 
				select(
				coin =
				banks_strikes 
				banks_demos
				banks_guerilla 
				banks_riots	
				logpop
				logrgdppc
				i.cheibub_democracy
				lmtnest
				maxlowx
				lnoilavg
				i.ncontig
				ins_time
				ins_time2
				ins_time3			
				)
				cluster(gwno)";


		//Running model
		quiet `model';		
				
		//Calculating and exporting marginal effects (95% CIs) for strikes
		margins, dydx(banks_strikes) 
				at(	(median) _continuous 
					ncontig=(0)
					multcoins=(0)
					occup=(0)	
					(p25) logtroopqual
					cheibub_democracy=(0)					
					banks_demos=(0)  
					banks_guerilla=(0)
					banks_riots=(0)				
					) 
				 level(95)
				 predict(pcond)
				 predict(psel)
				 post;
		parmest, saving("$path\margins_strike_CI95.dta", replace) format(estimate min95 max95);
		
		
		//Running model
		quiet `model';	
		
		//Calculating and exporting marginal effects (95% CIs) for demonstrations
		margins, 
				dydx(banks_demos)
				at((median) _continuous 
					ncontig=(0)
					multcoins=(0)
					occup=(0)	
					(p25) logtroopqual
					cheibub_democracy=(0)		
					banks_strikes=(0)  
					banks_guerilla=(0)
					banks_riots=(0)
					) 
				 level(95)
				 predict(pcond)
				 predict(psel)
				 post;
		parmest, saving("$path\margins_demo_CI95.dta", replace) format(estimate min95 max95);	
		
		
		//Running model
		quiet `model';	
		
		//Calculating and exporting marginal effects (95% CIs) for guerrilla attacks
		margins, 
				dydx(banks_guerilla)
				 at((median) _continuous 
					ncontig=(0)
					multcoins=(0)
					occup=(0)	
					(p25) logtroopqual
					cheibub_democracy=(0)		 
					banks_strikes=(0) 
					banks_demos=(0) 
					banks_riots=(0)
					) 
				 level(95)
				 predict(pcond)
				 predict(psel)
				 post;
		parmest, saving("$path\margins_guerr_CI95.dta", replace) format(estimate min95 max95);		
		
		
		//Running model
		quiet `model';	
		
		//Calculating and exporting marginal effects (95% CIs) for riots
		margins, 
				dydx(banks_riots)
				at((median) _continuous 
					ncontig=(0)
					multcoins=(0)
					occup=(0)	
					(p25) logtroopqual
					cheibub_democracy=(0)		
					banks_strikes=(0) 
					banks_demos=(0) 
					banks_guerilla=(0)
					) 
				 level(95)
				 predict(pcond)
				 predict(psel)
				 post;
		parmest, saving("$path\margins_riot_CI95.dta", replace) format(estimate min95 max95);		

		# delimit cr
				
		*Loading results datasets		
		use "$path\margins_demo_CI95.dta", clear
		
		foreach x in guerr riot strike {
		append using "$path\margins_`x'_CI95.dta" 		
		}		
		
		*Generating context variable
		gen coup=1 if regexm(parm, "^[1]._")
		gen coin=1 if regexm(parm, "^[2]._")
		
		*Generating resistance variable
		gen resist=1 if regexm(eq, "strik")
		replace resist=2 if regexm(eq, "demo")
		replace resist=3 if regexm(eq, "guer")
		replace resist=4 if regexm(eq, "riot")
			
		*Renaming variables with quantities of interest
		rename min95 lo
		rename max95 hi
		rename estimate mean
		
		
		*Context: Peace 
		
		*Generating x-axis
		gen xaxis_peace=1 if resist==1 & coin==1
		replace xaxis_peace=2 if resist==2 & coin==1
		replace xaxis_peace=3 if resist==3 & coin==1
		replace xaxis_peace=4 if resist==4 & coin==1	
		
		
		*Graph
		#delimit ;		
		twoway rspike hi lo xaxis_peace if resist==1, 
					 fcolor(black) lcolor(black) fintensity(100)
		  || scatter mean xaxis_peace if resist==1,
					 msymbol(o) msize(*1) mfcolor(white) mlcolor(black) mlwidth(.3) mlalign(outside)
		  || rspike hi lo xaxis_peace if resist==2, 
					 fcolor(black) lcolor(black) fintensity(100)
		  || scatter mean xaxis_peace if resist==2,
					 msymbol(s) msize(*1.5) mfcolor(white) mlcolor(black)				 
		  || rspike hi lo xaxis_peace if resist==3, 
					 fcolor(gray) lcolor(black) fintensity(100)
		  || scatter mean xaxis_peace if resist==3,
					 msymbol(o) msize(*1.5) mfcolor(black) mlcolor(black)
		  || rspike hi lo xaxis_peace if resist==4, 
					 fcolor(gray) lcolor(black) fintensity(100)
		  || scatter mean xaxis_peace if resist==4,
					 yline(0, lpattern(dot) lcolor(gs8))
					 msymbol(s) msize(*1.5) mfcolor(black) mlcolor(black)
					 xlabel(none) 
					 xscale(range(0.5 4.5) noline titlegap(*+10))
					 yscale(range( ) noline)				 
					 yticks(, tlength(*.5))
					 ylabel(, angle(horizontal) nogrid labsize(medium))
					 xlabel(1 "Strikes" 2 "Demonstrations" 3 `" "Guerrilla" "attacks" "' 4 "Riots", angle(horizontal) nogrid labsize(medium))
					 xtitle("Resistance type", size(medium) color(white)) 
					 ytitle("Pr(Insurgency)", size(medium))
					 ysize(1.5) xsize(2)
					 title("Stage 1: Insurgency risk during peace", size(*0.85) box bexpand margin(t=1.25))
					 legend(off)
					 scheme(s2mono) plotregion(lcolor(black)) graphregion(color(white) lcolor(white) lalign(outside))
					 saving("$path\heckman_peace", replace)				 
		; 
		#delimit cr		
		
		
		*Context: Counterinsurgency

		*Generating x-axis
		gen xaxis_insur=1 if resist==1 & coin==.
		replace xaxis_insur=2 if resist==2 & coin==.
		replace xaxis_insur=3 if resist==3 & coin==.
		replace xaxis_insur=4 if resist==4 & coin==.	
		
		
		*Graph
		#delimit ;		
		twoway rspike hi lo xaxis_insur if resist==1, 
					 fcolor(black) lcolor(black) fintensity(100)
		  || scatter mean xaxis_insur if resist==1,
					 msymbol(o) msize(*1) mfcolor(white) mlcolor(black) mlwidth(.3) mlalign(outside)
		  || rspike hi lo xaxis_insur if resist==2, 
					 fcolor(black) lcolor(black) fintensity(100)
		  || scatter mean xaxis_insur if resist==2,
					 msymbol(s) msize(*1.5) mfcolor(white) mlcolor(black)				 
		  || rspike hi lo xaxis_insur if resist==3, 
					 fcolor(gray) lcolor(black) fintensity(100)
		  || scatter mean xaxis_insur if resist==3,
					 msymbol(o) msize(*1.5) mfcolor(black) mlcolor(black)
		  || rspike hi lo xaxis_insur if resist==4, 
					 fcolor(gray) lcolor(black) fintensity(100)
		  || scatter mean xaxis_insur if resist==4,
					 yline(0, lpattern(dot) lcolor(gs8))
					 msymbol(s) msize(*1.5) mfcolor(black) mlcolor(black)
					 xlabel(none) 
					 xscale(range(0.5 4.5) noline titlegap(*+10))
					 yscale(range( ) noline)				 
					 yticks( , tlength(*.5))
					 ylabel(, angle(horizontal) nogrid labsize(medium))
					 xlabel(1 "Strikes" 2 "Demonstrations" 3 `" "Guerrilla" "attacks" "' 4 "Riots", angle(horizontal) nogrid labsize(medium))
					 xtitle("Resistance types", size(medium)) 
					 ytitle("Pr(Coup)", size(medium))
					 ysize(1.5) xsize(2)
					 title("Stage 2: Coup risk during insurgency", size(*0.85) box bexpand margin(t=1.25))
					 legend(off)
					 scheme(s2mono) plotregion(lcolor(black)) graphregion(color(white) lcolor(white) lalign(outside))
					 saving("$path\heckman_insurgency", replace)
		; 
		#delimit cr		
		
	*Combing graphs
	#delimit ;
	graph combine "$path\heckman_peace.gph" 
				  "$path\heckman_insurgency.gph" 
					,
				  cols(1)
				  ysize(1.5) xsize(1)
				  graphregion(color(white) lcolor(white) lalign(outside))
				  ;
	#delimit cr
	graph export "$path\figure5.pdf", replace as(pdf)	
	
	*Restoring dataset
	restore

		
	
********************************************************************************
*
* Results in Supporting Information
*
********************************************************************************
	
	****************************************************************************
	*Figure SI.1. Distribution of coup and resistance events in the data
	****************************************************************************	
	
	*Preserving dataset
	preserve
	
	*Keeping COIN observations
	keep if coin==1
	keep gwno year coup banks_guerilla_d banks_riots_d banks_strikes_d banks_demos_d

	*Number of observations in data set
	gen totalobs=_N

	*Number of observations of each threat type
	foreach var of varlist coup banks_guerilla_d banks_riots_d banks_strikes_d banks_demos_d {
	egen total_`var'=total(`var'), missing
	}
	
	*Calculating percentages for each threat type
	foreach var of varlist total*  {
	gen perc`var'=(`var'/totalobs)*100
	}	
	
	*Keeping first value
	keep in 1
	keep perc*

	*Renaming variables
	ds 
	foreach v of var `r(varlist)' {
           local new = substr("`v'", 11,.)
           rename `v' `new'
	}
	
	*Reshaping dataset
	gen help=1
	rename coup coup_d
	reshape long @_d, i(help) string
	rename _j var
	rename _d perc
	
	*Ordering variables for plotting
	gsort - perc
	gen yaxis=_n

	*Graph
	#delimit ;
		twoway dropline perc yaxis,
		horizontal
		msymbol(o) msize(*1.1) mfcolor(black) mlcolor(black) mlalign(center)
		lcolor(black) lwidth(*.9)
		ylabel(
				5 "Coup attempts"
				4 "Strikes" 
				3 "Riots" 
				2 "Demonstrations" 
				1 "Guerrilla attacks" 		
				, labgap(*3) angle(horizontal) noticks nogrid labsize(3))
		yscale(range(0 6) noline)	
		xscale(range(0 0) noline)
		xlabel(0(10)100, labsize(3) grid glwidth(*.4) glpattern(shortdash) glcolor(gs10) nogmin )
		xtick(0(5)100, tlength(*.5))
		ytitle("")
		xtitle("Percentage of years", size(3))	
		plotregion(lcolor(black) m(zero)) 
		graphregion(color(white) lcolor(white) lalign(outside))
	;
	#delimit cr	
	graph export "$path\figureSI1.pdf", as(pdf) replace

	*Restoring dataset
	restore	
	
	

********************************************************************************
*SI: Additional visualization of substantive effects
********************************************************************************
	
	****************************************************************************
	*Figure SI.2. Predicted probabilities for full range of resistance events 
	*during insurgencies
	****************************************************************************	
	
	*Preserving dataset
	preserve
	
	*Identifying observations in Model 3

	*Model 3
	#delimit ;
	logit 	coup
			banks_guerilla banks_riots 
			banks_strikes banks_demos
			time time2 time3
			changergdp
			logtroopqual
			cheibub_democracy
			ldis
			multcoins
			occup
			if coin == 1, 
			cluster(gwno)
	;
	# delimit cr

	*Empirical range of independent variables
	foreach var of varlist banks_guerilla banks_riots banks_strikes banks_demos {
	sum `var' if e(sample)
	gen `var'_max=`r(max)'
	}

	*Empirical range of independent variables
	foreach var of varlist banks_guerilla banks_riots banks_strikes banks_demos {
	gen `var'_empobs=`var' if e(sample)
	}

	*Construction x-axis (0 <-> 5)
	gen float xaxis=_n-1 in 1/56

	*Generating container variables for perdicted probabilities
	foreach var of varlist banks_guerilla banks_riots banks_strikes banks_demos {
	gen prlo1_`var'=.
	gen prhi1_`var'=.
	gen prpo1_`var'=.
	}

	*Calculating predicted probabilities from Model 3
	set seed 8413275
	#delimit ;
	estsimp 
	logit 	coup
			banks_guerilla banks_riots 
			banks_strikes banks_demos
			time time2 time3
			changergdp
			logtroopqual
			cheibub_democracy
			ldis
			multcoins
			occup
			if coin == 1, 
			cluster(gwno)
	;
	# delimit cr


	*Effect of guerrilla attacks
	setx banks_riots 0 banks_strikes 0 banks_demos 0 cheibub_democracy 0 occup 0 logtroopqual p25 (time time2 time3 changergdp ldis multcoins) median
	local a=0 
	while `a'<=55 {
		setx banks_guerilla `a'
		simqi, prval(1) genpr(pr1) listx
		_pctile pr1, p(2.5,97.5)	
		replace prlo1_banks_guerilla=r(r1) if xaxis==float(`a')
		replace prhi1_banks_guerilla=r(r2) if xaxis==float(`a')
		egen p = mean(pr1)
		replace prpo1_banks_guerilla = p if xaxis==float(`a')
		drop pr1 p
		local a=`a'+1
	}

	*Effect of riots
	setx banks_guerilla 0 banks_strikes 0 banks_demos 0 cheibub_democracy 0 occup 0 logtroopqual p25 (time time2 time3 changergdp ldis multcoins) median
	local a=0 
	while `a'<=55 {
		setx banks_riots `a'
		simqi, prval(1) genpr(pr1) listx
		_pctile pr1, p(2.5,97.5)	
		replace prlo1_banks_riots=r(r1) if xaxis==float(`a')
		replace prhi1_banks_riots=r(r2) if xaxis==float(`a')
		egen p = mean(pr1)
		replace prpo1_banks_riots = p if xaxis==float(`a')
		drop pr1 p
		local a=`a'+1
	}

	*Effect of strikes
	setx banks_guerilla 0 banks_riots 0 banks_demos 0 cheibub_democracy 0 occup 0 logtroopqual p25 (time time2 time3 changergdp ldis multcoins) median
	local a=0 
	while `a'<=55 {
		setx banks_strikes `a'
		simqi, prval(1) genpr(pr1) listx
		_pctile pr1, p(2.5,97.5)	
		replace prlo1_banks_strikes=r(r1) if xaxis==float(`a')
		replace prhi1_banks_strikes=r(r2) if xaxis==float(`a')
		egen p = mean(pr1)
		replace prpo1_banks_strikes = p if xaxis==float(`a')
		drop pr1 p
		local a=`a'+1
	}

	*Effect of demos
	setx banks_guerilla 0 banks_strikes 0 banks_riots 0 cheibub_democracy 0 occup 0 logtroopqual p25 (time time2 time3 changergdp ldis multcoins) median
	local a=0 
	while `a'<=55 {
		setx banks_demos `a'
		simqi, prval(1) genpr(pr1) listx
		_pctile pr1, p(2.5,97.5)	
		replace prlo1_banks_demos=r(r1) if xaxis==float(`a')
		replace prhi1_banks_demos=r(r2) if xaxis==float(`a')
		egen p = mean(pr1)
		replace prpo1_banks_demos = p if xaxis==float(`a')
		drop pr1 p
		local a=`a'+1
	}

	*Keeping predicted probabilities, x-values, and banks variables
	keep xaxis pr* banks_guerilla banks_riots banks_strikes banks_demos *_empobs *_max

	*Defining y-value for rug elements
	gen yrug=-.05

	*Defining y-values for vertical lines
	gen lineyval=-0.025 in 1
	replace lineyval=1 in 2

	*Graph for strikes
	#delimit ;		
	  twoway 
		  || rarea prlo1_banks_strikes prhi1_banks_strikes xaxis, 
					 fcolor(gs13) lcolor(gs13) fintensity(100)
		  || line prpo1_banks_strikes xaxis,
					 lpattern(solid) lcolor(black) lwidth(*.9)
		  || scatter yrug banks_strikes_empobs, 
					 msymbol("|") mcolor(black)
		  || line lineyval banks_strikes_max,
					 lpattern(dot) lcolor(black) lwidth(*3)					 
					 title("Strikes", size(*0.85) box bexpand margin(t=1.25))
					 xlabel(none) 
					 xscale(range(-0.5 5.5) noline titlegap(*+5))
					 yscale(range(-.075 1) noline)				 
					 ylabel(0(.1)1, angle(horizontal) nogrid labsize(medium))
					 xlabel(0(5)55, angle(horizontal) nogrid labsize(medium))
					 xticks(0(1)55, tlength(*0.5))
					 xtitle(" ", size(medium)) 
					 ytitle("Pr(Y=1)", size(medium))
					 ysize(1.5) xsize(2)
					 subtitle(,size(2.5) lcolor(black))
					 legend(order(2 "Point estimate" 1 "95% CI" 3 "Empirical observations" 4 "Max. coverage in dataset") 
							position(5) ring(0) bmargin(0) col(1) row(4) 
							size(small) keygap(*0.5) symxsize(*0.3) symysize(*0.25) region(lcolor(black)))
					 scheme(s2mono) plotregion(lcolor(black)) graphregion(color(white) lcolor(white) lalign(outside))
					 saving("$path\fullrange_strike", replace); 
	#delimit cr


	*Graph for demonstrations
	#delimit ;		
	  twoway || rarea prlo1_banks_demos prhi1_banks_demos xaxis, 
					 fcolor(gs13) lcolor(gs13) fintensity(100)	
			 || line prpo1_banks_demos xaxis,
					 lpattern(solid) lcolor(black) lwidth(*.9)
			 || scatter yrug banks_demos_empobs, 
					 msymbol("|") mcolor(black)
			 || line lineyval banks_demos_max,
					 lpattern(dot) lcolor(black) lwidth(*3)	
					 title("Demonstrations", size(*0.85) box bexpand margin(t=1.25))
					 xlabel(none) 
					 xscale(range(-0.5 5.5) noline titlegap(*+5))
					 yscale(range(-.075 1) noline)				 
					 ylabel(0(.1)1, angle(horizontal) nogrid labsize(medium))
					 xlabel(0(5)55, angle(horizontal) nogrid labsize(medium))
					 xticks(0(1)55, tlength(*0.5))
					 xtitle(" ", size(medium)) 
					 ytitle("Pr(Y=1)", size(medium))
					 ysize(1.5) xsize(2)
					 subtitle(,size(2.5) lcolor(black))
					 legend(off)
					 scheme(s2mono) plotregion(lcolor(black)) graphregion(color(white) lcolor(white) lalign(outside))
					 saving("$path\fullrange_demon", replace);  
	#delimit cr

	*Graph for guerrilla attacks
	#delimit ;		
	  twoway 
		  || rarea prlo1_banks_guerilla prhi1_banks_guerilla xaxis, 
					 fcolor(gs13) lcolor(gs13) fintensity(100)
		  || line prpo1_banks_guerilla xaxis,
					 lpattern(solid) lcolor(black) lwidth(*.9)
		  || scatter yrug banks_guerilla_empobs, 
					 msymbol("|") mcolor(black)
		  || line lineyval banks_guerilla_max,
					 lpattern(dot) lcolor(black) lwidth(*3)					 
					 title("Guerrilla attacks", size(*0.85) box bexpand margin(t=1.25))
					 xlabel(none) 
					 xscale(range(-0.5 5.5) noline titlegap(*+5))
					 yscale(range(-.075 1) noline)				 
					 ylabel(0(.1)1, angle(horizontal) nogrid labsize(medium))
					 xlabel(0(5)55, angle(horizontal) nogrid labsize(medium))
					 xticks(0(1)55, tlength(*0.5))
					 xtitle(" ", size(medium)) 
					 ytitle("Pr(Y=1)", size(medium))
					 ysize(1.5) xsize(2)
					 subtitle(,size(2.5) lcolor(black))
					 legend(off)
					 scheme(s2mono) plotregion(lcolor(black)) graphregion(color(white) lcolor(white) lalign(outside))
					 saving("$path\fullrange_guerrilla", replace); 
	#delimit cr

	*Graph for riots 
	#delimit ;		
	  twoway 
		  || rarea prlo1_banks_riots prhi1_banks_riots xaxis, 
					 fcolor(gs13) lcolor(gs13) fintensity(100)
		  || line prpo1_banks_riots xaxis,
					 lpattern(solid) lcolor(black) lwidth(*.9)
		  || scatter yrug banks_riots_empobs, 
					 msymbol("|") mcolor(black)
		  || line lineyval banks_riots_max,
					 lpattern(dot) lcolor(black) lwidth(*3)					 
					 title("Riots", size(*0.85) box bexpand margin(t=1.25))
					 xlabel(none) 
					 xscale(range(-0.5 5.5) noline titlegap(*+5))
					 yscale(range(-.075 1) noline)				 
					 ylabel(0(.1)1, angle(horizontal) nogrid labsize(medium))
					 xlabel(0(5)55, angle(horizontal) nogrid labsize(medium))
					 xticks(0(1)55, tlength(*0.5))
					 xtitle("Number of resistance events", size(medium)) 
					 ytitle("Pr(Y=1)", size(medium))
					 ysize(1.5) xsize(2)
					 subtitle(,size(2.5) lcolor(black))
					 legend(off)
					 scheme(s2mono) plotregion(lcolor(black)) graphregion(color(white) lcolor(white) lalign(outside))
					 saving("$path\fullrange_riot", replace); 
	#delimit cr

	*Combing graphs
	#delimit ;
	graph combine "$path\fullrange_strike.gph" 
				  "$path\fullrange_demon.gph" 
				  "$path\fullrange_guerrilla.gph" 
				  "$path\fullrange_riot.gph",
				  cols(1)
				  ysize(2.5) xsize(1)
				  graphregion(color(white) lcolor(white) lalign(outside))
				  ;
	#delimit cr
	graph export "$path\figureSI2.pdf", replace as(pdf)
	
	*Restoring dataset
	restore
	
	
	
********************************************************************************	
*	
********************************************************************************		
	
	*Closing graph windows
	graph close _all 
	
	*Clearing data
	clear all
	
	*End recording log file
	log close	
